<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="format-detection" content="telephone=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><link rel="icon" href="/blog/blog/images/icons/favicon-16x16.png?v=2.6.2" type="image/png" sizes="16x16"><link rel="icon" href="/blog/blog/images/icons/favicon-32x32.png?v=2.6.2" type="image/png" sizes="32x32"><meta name="description" content=".back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;} .back-top&gt;a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height:">
<meta property="og:type" content="article">
<meta property="og:title" content="C&#x2F;C++力扣LeetCode转载">
<meta property="og:url" content="https://bruce-jj.gitee.io/blog/2021/03/11/C_C++%E5%8A%9B%E6%89%A3LeetCode%E8%BD%AC%E8%BD%BD/index.html">
<meta property="og:site_name" content="斯人若彩虹 遇上方知有">
<meta property="og:description" content=".back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;} .back-top&gt;a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height:">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-03-11T09:51:27.000Z">
<meta property="article:modified_time" content="2021-03-24T09:25:44.000Z">
<meta property="article:author" content="何水浪">
<meta property="article:tag" content="C语言">
<meta name="twitter:card" content="summary"><title>C/C++力扣LeetCode转载 | 斯人若彩虹 遇上方知有</title><link ref="canonical" href="https://bruce-jj.gitee.io/blog/2021/03/11/C_C++%E5%8A%9B%E6%89%A3LeetCode%E8%BD%AC%E8%BD%BD/"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.12.1/css/all.min.css" type="text/css"><link rel="stylesheet" href="/blog/css/index.css?v=2.6.2"><script>var Stun = window.Stun || {};
var CONFIG = {
  root: '/blog/',
  algolia: undefined,
  assistSearch: undefined,
  fontIcon: {"prompt":{"success":"fas fa-check-circle","info":"fas fa-arrow-circle-right","warning":"fas fa-exclamation-circle","error":"fas fa-times-circle"},"copyBtn":"fas fa-copy"},
  sidebar: {"offsetTop":"20px","tocMaxDepth":6},
  header: {"enable":true,"showOnPost":true,"scrollDownIcon":false},
  postWidget: {"endText":true},
  nightMode: {"enable":true},
  back2top: {"enable":true},
  codeblock: {"style":"default","highlight":"light","wordWrap":false},
  reward: false,
  fancybox: false,
  zoomImage: {"gapAside":"20px"},
  galleryWaterfall: undefined,
  lazyload: false,
  pjax: undefined,
  externalLink: {"icon":{"enable":true,"name":"fas fa-external-link-alt"}},
  shortcuts: undefined,
  prompt: {"copyButton":"复制","copySuccess":"复制成功","copyError":"复制失败"},
  sourcePath: {"js":"js","css":"css","images":"images"},
};

window.CONFIG = CONFIG;</script><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/blog/atom.xml" title="斯人若彩虹 遇上方知有" type="application/atom+xml">
</head><body><div class="container" id="container"><header class="header" id="header"><div class="header-inner"><nav class="header-nav header-nav--fixed"><div class="header-nav-inner"><div class="header-nav-menubtn"><i class="fas fa-bars"></i></div><div class="header-nav-menu"><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/"><span class="header-nav-menu-item__icon"><i class="fas fa-home"></i></span><span class="header-nav-menu-item__text">首页</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/archives/"><span class="header-nav-menu-item__icon"><i class="fas fa-folder-open"></i></span><span class="header-nav-menu-item__text">归档</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/categories/"><span class="header-nav-menu-item__icon"><i class="fas fa-layer-group"></i></span><span class="header-nav-menu-item__text">分类</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/tags/"><span class="header-nav-menu-item__icon"><i class="fas fa-tags"></i></span><span class="header-nav-menu-item__text">标签</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/friends/"><span class="header-nav-menu-item__icon"><i class="fas fa-link"></i></span><span class="header-nav-menu-item__text">友链</span></a></div></div><div class="header-nav-search"><span class="header-nav-search__icon"><i class="fas fa-search"></i></span><span class="header-nav-search__text">搜索</span></div><div class="header-nav-mode"><div class="mode"><div class="mode-track"><span class="mode-track-moon"></span><span class="mode-track-sun"></span></div><div class="mode-thumb"></div></div></div></div></nav><div class="header-banner"><div class="header-banner-info"><div class="header-banner-info__title">斯人若彩虹 遇上方知有</div><div class="header-banner-info__subtitle"></div></div></div></div></header><main class="main" id="main"><div class="main-inner"><div class="content-wrap" id="content-wrap"><div class="content" id="content"><!-- Just used to judge whether it is an article page--><div id="is-post"></div><div class="post"><header class="post-header"><h1 class="post-title">C/C++力扣LeetCode转载</h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-11</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-03-24</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">7.1k</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">53分</span></span></div></header><div class="post-body"><!-- 页面必备样式 -->
<style>
.back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;}
.back-top>a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height: 50px;border-radius: 50px;background-color:rgba(255,255,255,0.5);}
</style>
<div class="back-top">
    <a href="#top"> >返回顶部< </a>
</div>
<!-- toc -->
<!-- 必备样式结束 -->


        <h2 id="27-原地移出某数后的数组"   >
          <a href="#27-原地移出某数后的数组" class="heading-link"><i class="fas fa-link"></i></a><a href="#27-原地移出某数后的数组" class="headerlink" title="27.原地移出某数后的数组"></a>27.原地移出某数后的数组</h2>
      <p><strong>给你一个数组 nums 和一个值 val</strong>，你<strong>需要 原地 移除所有数值等于 val 的元素</strong>，<strong>并返回移除后数组的新长度</strong>。不要使用额外的数组空间，你必须仅使用 O(1) 额外空间并 原地 修改输入数组。元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。</p>
<p>说明:<br>为什么返回数值是整数，但输出的答案是数组呢?<br>请注意，输入数组是以「引用」方式传递的，这意味着在函数里修改输入数组对于调用者是可见的。<br>你可以想象内部操作如下:</p>
<p>// nums 是以“引用”方式传递的。也就是说，不对实参作任何拷贝<br>int len = removeElement(nums, val);</p>
<p>// 在函数里修改输入数组对于调用者是可见的。<br>// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。<br>for (int i = 0; i &lt; len; i++) {<br>    print(nums[i]);<br>}</p>
<p>示例 1：</p>
<p>输入：nums = [3,2,2,3], val = 3<br>输出：2, nums = [2,2]<br>解释：<strong>函数应该返回新的长度 2</strong>, <strong>并且 nums 中的前两个元素均为 2</strong>。你不需要考虑数组中超出新长度后面的元素。例如，函数返回的新长度为 2 ，而 nums = [2,2,3,3] 或 nums = [2,2,0,0]，也会被视作正确答案。<br>示例 2：</p>
<p>输入：nums = [0,1,2,2,3,0,4,2], val = 2<br>输出：5, nums = [0,1,4,0,3]<br>解释：函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。</p>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/remove-element" >https://leetcode-cn.com/problems/remove-element</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">removeElement</span><span class="params">(vector&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="keyword">int</span> val)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(nums.<span class="built_in">size</span>() == <span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        <span class="comment">//一个指针指向头 一个指向尾部</span></span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">0</span>, index = nums.<span class="built_in">size</span>()<span class="number">-1</span>;</span><br><span class="line">        <span class="keyword">while</span>(i &lt;= index) </span><br><span class="line">        &#123;</span><br><span class="line">			<span class="comment">//如果当前是要删除的值</span></span><br><span class="line">            <span class="keyword">if</span>(nums[i] == val)</span><br><span class="line">            &#123;</span><br><span class="line">           	 	<span class="comment">//将该数字与最后一个数字交换</span></span><br><span class="line">            	<span class="comment">//同时数组缩减</span></span><br><span class="line">                <span class="keyword">int</span> temp = nums[index];</span><br><span class="line">                nums[index] = nums[i];</span><br><span class="line">                nums[i] = temp;</span><br><span class="line">                --index;</span><br><span class="line">            &#125;</span><br><span class="line">            	<span class="comment">//如果不是要删除的值 继续往后推进</span></span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">                ++i;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> i;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>

<p>作者：Xiaohu9527<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/remove-element/solution/cyi-ci-li-bian-bu-xu-yao-bao-li-gan-jue-82cz7/" >https://leetcode-cn.com/problems/remove-element/solution/cyi-ci-li-bian-bu-xu-yao-bao-li-gan-jue-82cz7/</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>来源：力扣（LeetCode）<br>著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。</p>

        <h2 id="58-返回最后一个单词的长度"   >
          <a href="#58-返回最后一个单词的长度" class="heading-link"><i class="fas fa-link"></i></a><a href="#58-返回最后一个单词的长度" class="headerlink" title="58.返回最后一个单词的长度"></a>58.返回最后一个单词的长度</h2>
      <p><strong>给你一个字符串 s，由若干单词组成，单词之间用空格隔开。<strong><strong>返回字符串中最后一个单词的长度。</strong></strong>如果不存在</strong>最后一个单词，<strong>请返回 0</strong> 。<strong>（注意本题最后 可能出现连续的空格）</strong>单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。</p>
<p>示例 1：</p>
<p>输入：s = “Hello World”<br>输出：5<br>示例 2：</p>
<p>输入：s = “ “<br>输出：0</p>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/length-of-last-word" >https://leetcode-cn.com/problems/length-of-last-word</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
</p>

<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">lengthOfLastWord</span><span class="params">(string s)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> count = <span class="number">0</span>, end = s.<span class="built_in">size</span>()<span class="number">-1</span>;</span><br><span class="line">        <span class="comment">//直到最后一个变成第一个</span></span><br><span class="line">        <span class="comment">//未遇到第一个字母前 若有连续的空格 直接跳过</span></span><br><span class="line">        <span class="keyword">while</span>(end &gt;= <span class="number">0</span> &amp;&amp; s[end] == <span class="string">&#x27; &#x27;</span>)  </span><br><span class="line">			--end;</span><br><span class="line">		<span class="comment">//如果没有遇到最后一个字母前的空格 就count++ 且每次指针 end--</span></span><br><span class="line">        <span class="keyword">while</span>(end &gt;= <span class="number">0</span> &amp;&amp; s[end--] != <span class="string">&#x27; &#x27;</span>)</span><br><span class="line">            ++count ;</span><br><span class="line">        <span class="keyword">return</span> count;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="动态规划实现斐波那契数列"   >
          <a href="#动态规划实现斐波那契数列" class="heading-link"><i class="fas fa-link"></i></a><a href="#动态规划实现斐波那契数列" class="headerlink" title="动态规划实现斐波那契数列"></a>动态规划实现斐波那契数列</h2>
      <p>假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢？注意：给定 n 是一个正整数。</p>
<p>示例 1：<br>输入： 2<br>输出： 2<br>解释： 有两种方法可以爬到楼顶。</p>
<ol>
<li> 1 阶 + 1 阶</li>
<li> 2 阶</li>
</ol>
<p>示例 2：<br>输入： 3<br>输出： 3<br>解释： 有三种方法可以爬到楼顶。</p>
<ol>
<li> 1 阶 + 1 阶 + 1 阶</li>
<li> 1 阶 + 2 阶</li>
<li> 2 阶 + 1 阶</li>
</ol>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/climbing-stairs" >https://leetcode-cn.com/problems/climbing-stairs</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
</p>

<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//一定看懂本题的动态规划</span></span><br><span class="line"><span class="comment">//利用dp[i]将可能用到的值存起来 return dp[n]</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">climbStairs</span><span class="params">(<span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">		<span class="function">vector&lt;<span class="keyword">int</span>&gt; <span class="title">dp</span><span class="params">(n+<span class="number">3</span>, <span class="number">0</span>)</span></span>;  <span class="comment">//元素个数为n+3 默认为0</span></span><br><span class="line">		dp[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">		dp[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line">		dp[<span class="number">2</span>] = <span class="number">2</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//用数组把值存储起来 如果下次需要 直接调用相应的数组值即可 </span></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">3</span>; i&lt;=n; i++)&#123;</span><br><span class="line">			dp[i] = dp[i<span class="number">-1</span>] + dp[i<span class="number">-2</span>];</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> dp[n];</span><br><span class="line">    &#125;</span><br><span class="line">   </span><br></pre></td></tr></table></div></figure>


        <h2 id="合并有序数组使其有序"   >
          <a href="#合并有序数组使其有序" class="heading-link"><i class="fas fa-link"></i></a><a href="#合并有序数组使其有序" class="headerlink" title="合并有序数组使其有序"></a>合并有序数组使其有序</h2>
      <p>给你两个有序整数数组 nums1 和 nums2，请你将 nums2 合并到 nums1 中，使 nums1 成为一个有序数组。 （本来都有序，想要合并之后仍有序）初始化 nums1 和 nums2 的元素数量分别为 m 和 n 。你可以假设 nums1 的空间大小等于 m + n，这样它就有足够的空间保存来自 nums2 的元素。</p>
<p>示例 1：<br>输入：nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3<br>输出：[1,2,2,3,5,6]</p>
<p>示例 2：<br>输入：nums1 = [1], m = 1, nums2 = [], n = 0<br>输出：[1]</p>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/merge-sorted-array" >https://leetcode-cn.com/problems/merge-sorted-array</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">merge</span><span class="params">(vector&lt;<span class="keyword">int</span>&gt;&amp; nums1, <span class="keyword">int</span> m, vector&lt;<span class="keyword">int</span>&gt;&amp; nums2, <span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">    	<span class="comment">//从将nums2的全部内容 排在nums1的数组后面</span></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> k=m,j=<span class="number">0</span>; k&lt;m+n; k++,j++)&#123;</span><br><span class="line">        	nums1[k] = nums2[j];</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//整体对nums1第一个元素 以及加进来之后的最后一个元素进行排序即可 注意排序的长度是容纳n1 n2的长度之和</span></span><br><span class="line">		<span class="built_in">sort</span>(nums1.<span class="built_in">begin</span>(), nums1.<span class="built_in">begin</span>()+(m+n));</span><br><span class="line">		 </span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="找到值的索引-或者理应插入的位置"   >
          <a href="#找到值的索引-或者理应插入的位置" class="heading-link"><i class="fas fa-link"></i></a><a href="#找到值的索引-或者理应插入的位置" class="headerlink" title="找到值的索引 或者理应插入的位置"></a>找到值的索引 或者理应插入的位置</h2>
      <p><strong>给定一个排序数组和一个目标值</strong>，在<strong>数组中找到目标值，并返回其索引</strong>。<strong>如果目标值不存在于数组中，返回它将会被按顺序插入的位置。</strong>你可以假设数组中无重复元素。</p>
<p>示例 1:</p>
<p>输入: [1,3,5,6], 5<br>输出: 2<br>示例 2:</p>
<p>输入: [1,3,5,6], 2<br>输出: 1<br>示例 3:</p>
<p>输入: [1,3,5,6], 7<br>输出: 4<br>示例 4:</p>
<p>输入: [1,3,5,6], 0<br>输出: 0</p>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/search-insert-position" >https://leetcode-cn.com/problems/search-insert-position</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">searchInsert</span><span class="params">(vector&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="keyword">int</span> target)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">int</span> pos = <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">int</span> end = nums.<span class="built_in">size</span>();</span><br><span class="line">		</span><br><span class="line">        <span class="comment">//处理特殊情况 当小于第一个或者大于最后一个时</span></span><br><span class="line">		<span class="keyword">if</span>(target &lt; nums[<span class="number">0</span>])&#123;</span><br><span class="line">			<span class="keyword">return</span> pos;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">if</span>(target &gt; nums[end<span class="number">-1</span>])&#123;</span><br><span class="line">			pos = end;</span><br><span class="line">			<span class="keyword">return</span> pos;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">        <span class="comment">//如果中间有 那么返回该位置</span></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;end; i++)&#123;</span><br><span class="line">			<span class="keyword">if</span>(target == nums[i])&#123;</span><br><span class="line">				pos = i;</span><br><span class="line">				<span class="keyword">return</span> pos;	</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//如果下一个比它大 则返回下一个位置</span></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;end; i++)&#123;</span><br><span class="line">			<span class="keyword">if</span>(target &gt;nums[i] &amp;&amp; target &lt; nums[i+<span class="number">1</span>])&#123;</span><br><span class="line">				pos = i+<span class="number">1</span>;</span><br><span class="line">				<span class="keyword">return</span> pos;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> pos;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="121-买卖股票的最佳时机"   >
          <a href="#121-买卖股票的最佳时机" class="heading-link"><i class="fas fa-link"></i></a><a href="#121-买卖股票的最佳时机" class="headerlink" title="121. 买卖股票的最佳时机"></a>121. 买卖股票的最佳时机</h2>
      <p>给定一个数组 prices ，它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。<br>你只能选择 某一天 买入这只股票，并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润<br>返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润，返回 0 。</p>
<p><strong>示例 1：</strong><br>输入：[7,1,5,3,6,4]<br>输出：5<br>解释：在第 2 天（股票价格 = 1）的时候买入，在第 5 天（股票价格 = 6）的时候卖出，最大利润 = 6-1 = 5 。<br>     注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格；同时，你不能在买入前卖出股票。</p>
<p><strong>示例 2：</strong><br>输入：prices = [7,6,4,3,1]<br>输出：0<br>解释：在这种情况下, 没有交易完成, 所以最大利润为 0。</p>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock" >https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">maxProfit</span><span class="params">(vector&lt;<span class="keyword">int</span>&gt;&amp; prices)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">int</span> profit=<span class="number">0</span>, minValue=prices[<span class="number">0</span>]; <span class="comment">//假设第一个是最小值</span></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>; i&lt;prices.<span class="built_in">size</span>(); i++)&#123; <span class="comment">//从第二个到最后一个开始 </span></span><br><span class="line">			minValue = <span class="built_in">min</span>(minValue, prices[i]); <span class="comment">//每次比较minValue与当前值的最小值 </span></span><br><span class="line">			profit = <span class="built_in">max</span>(profit, prices[i]-minValue); <span class="comment">//利润就是当前值减去最小值的最大差值 </span></span><br><span class="line">		&#125; </span><br><span class="line">		<span class="keyword">return</span> profit;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="202-快乐数"   >
          <a href="#202-快乐数" class="heading-link"><i class="fas fa-link"></i></a><a href="#202-快乐数" class="headerlink" title="202. 快乐数"></a>202. 快乐数</h2>
      <p>编写一个算法来判断一个数 n 是不是快乐数。<br>「快乐数」定义为：<br>对于一个正整数，每一次将该数替换为它每个位置上的数字的平方和。然后重复这个过程直到这个数变为 1，也可能是 无限循环 但始终变不到 1。<br>如果 可以变为  1，那么这个数就是快乐数。<br>如果 n 是快乐数就返回 true ；不是，则返回 false 。</p>
<p><strong>示例 1：</strong><br>输入：19<br>输出：true<br>解释：<br>12 + 92 = 82<br>82 + 22 = 68<br>62 + 82 = 100<br>12 + 02 + 02 = 1</p>
<p><strong>示例 2：</strong><br>输入：n = 2<br>输出：false</p>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/happy-number" >https://leetcode-cn.com/problems/happy-number</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="comment">//拿到相应的各数之和函数</span></span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">getNum</span><span class="params">(<span class="keyword">int</span> n)</span></span>&#123;</span><br><span class="line">		<span class="keyword">int</span> sum=<span class="number">0</span>;</span><br><span class="line">		<span class="keyword">while</span>(n)&#123;</span><br><span class="line">			sum += (<span class="built_in">pow</span>(n%<span class="number">10</span>, <span class="number">2</span>));</span><br><span class="line">			n /= <span class="number">10</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> sum;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">bool</span> <span class="title">isHappy</span><span class="params">(<span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">		set&lt;<span class="keyword">int</span>&gt; s; </span><br><span class="line">        <span class="comment">//定义一个集合 因为如果结果不为1 相加一定有重复的数字</span></span><br><span class="line">		</span><br><span class="line">		<span class="keyword">while</span>(<span class="number">1</span>)&#123;     <span class="comment">//一直循环</span></span><br><span class="line">			</span><br><span class="line">			<span class="keyword">int</span> sum = <span class="built_in">getNum</span>(n);</span><br><span class="line">            <span class="comment">//每次将结果放入集合之前 先判断集合是否已经存在该元素</span></span><br><span class="line"></span><br><span class="line">        	<span class="comment">//如果已经出现了 说明结果肯定不会等于1</span></span><br><span class="line">            <span class="keyword">if</span>(s.<span class="built_in">count</span>(sum) == <span class="number">1</span>)&#123;</span><br><span class="line">				<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">            <span class="comment">//如果没有重复 则加入</span></span><br><span class="line">			s.<span class="built_in">insert</span>(sum);</span><br><span class="line">			</span><br><span class="line">            <span class="comment">//如果结果等于1 则返回true</span></span><br><span class="line">            <span class="keyword">if</span>(sum == <span class="number">1</span>)&#123;</span><br><span class="line">				<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="comment">//对每一次得到的sum重复计算！</span></span><br><span class="line">            n = sum;	</span><br><span class="line">		&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="136-只出现一次的数字"   >
          <a href="#136-只出现一次的数字" class="heading-link"><i class="fas fa-link"></i></a><a href="#136-只出现一次的数字" class="headerlink" title="136. 只出现一次的数字"></a>136. 只出现一次的数字</h2>
      <p>给定一个非空整数数组，除了某个元素只出现一次以外，其余每个元素均出现两次。找出那个只出现了一次的元素。</p>
<p><strong>示例 1:</strong><br>输入: [2,2,1]<br>输出: 1</p>
<p><strong>示例 2:</strong><br>输入: [4,1,2,1,2]<br>输出: 4</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">singleNumber</span><span class="params">(vector&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">           set&lt;<span class="keyword">int</span>&gt; s;</span><br><span class="line">           <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">           <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;nums.<span class="built_in">size</span>(); i++)&#123;</span><br><span class="line">           		<span class="keyword">if</span>(s.<span class="built_in">count</span>(nums[i]) == <span class="number">1</span>)&#123; <span class="comment">//如果集合中有这个元素 那么删除它</span></span><br><span class="line">           			s.<span class="built_in">erase</span>(nums[i]);</span><br><span class="line">				&#125;<span class="keyword">else</span>&#123;<span class="comment">//如果集合中没有这个元素 就添加进去</span></span><br><span class="line">					s.<span class="built_in">insert</span>(nums[i]);</span><br><span class="line">				&#125;</span><br><span class="line">		   &#125;</span><br><span class="line">		   set&lt;<span class="keyword">int</span>&gt;::iterator it; </span><br><span class="line">		   <span class="keyword">for</span>(it=s.<span class="built_in">begin</span>();it != s.<span class="built_in">end</span>(); it++)&#123;</span><br><span class="line">		   		res = *it;<span class="comment">//最后剩余的元素 一定是不重复的元素</span></span><br><span class="line">		   &#125;</span><br><span class="line">           <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>

        <h2 id="204-计数质数"   >
          <a href="#204-计数质数" class="heading-link"><i class="fas fa-link"></i></a><a href="#204-计数质数" class="headerlink" title="204. 计数质数"></a>204. 计数质数</h2>
      <p>统计所有小于非负整数 n 的质数的数量。</p>
<p>示例 1：<br>输入：n = 10<br>输出：4<br>解释：小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。</p>
<p>示例 2：<br>输入：n = 0<br>输出：0</p>
<p>示例 3：<br>输入：n = 1<br>输出：0</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">isPrime</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;</span><br><span class="line">		<span class="comment">//在0~根号x的范围内 即可判断是否是质数</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">2</span>; i * i &lt;= x; ++i) &#123;  </span><br><span class="line">            <span class="keyword">if</span> (x % i == <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//在C++中 true的数字代表1 多少个true相加</span></span><br><span class="line">	<span class="comment">//就代表有多少个质数 从2~n-1判断即可</span></span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">countPrimes</span><span class="params">(<span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">2</span>; i &lt; n; ++i) &#123;</span><br><span class="line">            ans += <span class="built_in">isPrime</span>(i);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="217-存在重复元素"   >
          <a href="#217-存在重复元素" class="heading-link"><i class="fas fa-link"></i></a><a href="#217-存在重复元素" class="headerlink" title="217. 存在重复元素"></a>217. 存在重复元素</h2>
      <p>给定一个整数数组，判断是否存在重复元素。</p>
<p>如果存在一值在数组中出现至少两次，函数返回 true 。如果数组中每个元素都不相同，则返回 false 。</p>
<p>示例 1:<br>输入: [1,2,3,1]<br>输出: true</p>
<p>示例 2:<br>输入: [1,2,3,4]<br>输出: false</p>
<p>示例 3:<br>输入: [1,1,1,3,3,4,3,2,4,2]<br>输出: true</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">containsDuplicate</span><span class="params">(vector&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">		set&lt;<span class="keyword">int</span>&gt; s;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;nums.<span class="built_in">size</span>(); i++)&#123;</span><br><span class="line">			</span><br><span class="line">			<span class="comment">//如果集合中存在重复元素 直接return真</span></span><br><span class="line">			<span class="keyword">if</span>(s.<span class="built_in">count</span>(nums[i])  == <span class="number">1</span>)&#123; </span><br><span class="line">				<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">			<span class="comment">//否则持续向集合中 添加数组中的元素</span></span><br><span class="line">			s.<span class="built_in">insert</span>(nums[i]);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="258-各位相加"   >
          <a href="#258-各位相加" class="heading-link"><i class="fas fa-link"></i></a><a href="#258-各位相加" class="headerlink" title="258. 各位相加"></a>258. 各位相加</h2>
      <p>给定一个非负整数 num，反复将各个位上的数字相加，直到结果为一位数。</p>
<p>示例:<br>输入: 38<br>输出: 2<br>解释: 各位相加的过程为：3 + 8 = 11, 1 + 1 = 2。 由于 2 是一位数，所以返回 2。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="comment">//计算各数之和的方法</span></span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">getNum</span><span class="params">(<span class="keyword">int</span> num)</span></span>&#123;</span><br><span class="line">		<span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">while</span>(num)&#123;</span><br><span class="line">			res += num%<span class="number">10</span>;</span><br><span class="line">			num /= <span class="number">10</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> res;	</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">int</span> <span class="title">addDigits</span><span class="params">(<span class="keyword">int</span> num)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">		<span class="comment">//不到跳出循环的条件不罢休</span></span><br><span class="line">		<span class="keyword">while</span>(<span class="number">1</span>)&#123;</span><br><span class="line">			<span class="comment">//算出num的各数之和</span></span><br><span class="line">			res = <span class="built_in">getNum</span>(num); </span><br><span class="line">			<span class="comment">//直到算到个数之和小于10</span></span><br><span class="line">			<span class="keyword">if</span>(res&lt;<span class="number">10</span>)&#123;</span><br><span class="line">				<span class="keyword">return</span> res;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="comment">//下一次计算新结果的个数之和</span></span><br><span class="line">			num = res;</span><br><span class="line">		&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="28-实现-strStr"   >
          <a href="#28-实现-strStr" class="heading-link"><i class="fas fa-link"></i></a><a href="#28-实现-strStr" class="headerlink" title="28.实现 strStr()"></a>28.实现 strStr()</h2>
      <p>给定一个 haystack 字符串和一个 needle 字符串，在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在，则返回  -1。</p>
<p><strong>示例 1:</strong><br>输入: haystack = “hello”, needle = “ll”<br>输出: 2</p>
<p><strong>示例 2:</strong><br>输入: haystack = “aaaaa”, needle = “bba”<br>输出: -1</p>
<p>当 needle 是空字符串时，我们应当返回什么值呢？这是一个在面试中很好的问题。<br>对于本题而言，当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。</p>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/implement-strstr" >https://leetcode-cn.com/problems/implement-strstr</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">strStr</span><span class="params">(string haystack, string needle)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> pos = <span class="number">0</span>;</span><br><span class="line">		<span class="comment">//如果是空字符串 返回0</span></span><br><span class="line">        <span class="keyword">if</span>(needle == <span class="string">&quot; &quot;</span>)&#123; </span><br><span class="line">            <span class="keyword">return</span> pos;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			<span class="comment">//如果不为空 则找到对应的位置</span></span><br><span class="line">            pos = haystack.<span class="built_in">find</span>(needle);</span><br><span class="line">			<span class="comment">//如果找得到 返回位置</span></span><br><span class="line">            <span class="keyword">if</span>( pos != string::npos)&#123;</span><br><span class="line">                <span class="keyword">return</span> pos;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">		<span class="comment">//如果找不到 就返回-1</span></span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="83-删除排序链表中的重复元素"   >
          <a href="#83-删除排序链表中的重复元素" class="heading-link"><i class="fas fa-link"></i></a><a href="#83-删除排序链表中的重复元素" class="headerlink" title="83. 删除排序链表中的重复元素"></a>83. 删除排序链表中的重复元素</h2>
      <p>给定一个排序链表，删除所有重复的元素，使得每个元素只出现一次。</p>
<p><strong>示例 1:</strong><br>输入: 1-&gt;1-&gt;2<br>输出: 1-&gt;2</p>
<p><strong>示例 2:</strong><br>输入: 1-&gt;1-&gt;2-&gt;3-&gt;3<br>输出: 1-&gt;2-&gt;3</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Definition for singly-linked list.</span></span><br><span class="line"><span class="comment"> * struct ListNode &#123;</span></span><br><span class="line"><span class="comment"> *     int val;</span></span><br><span class="line"><span class="comment"> *     ListNode *next;</span></span><br><span class="line"><span class="comment"> *     ListNode() : val(0), next(nullptr) &#123;&#125;</span></span><br><span class="line"><span class="comment"> *     ListNode(int x) : val(x), next(nullptr) &#123;&#125;</span></span><br><span class="line"><span class="comment"> *     ListNode(int x, ListNode *next) : val(x), next(next) &#123;&#125;</span></span><br><span class="line"><span class="comment"> * &#125;;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function">ListNode* <span class="title">deleteDuplicates</span><span class="params">(ListNode* head)</span> </span>&#123;</span><br><span class="line">		ListNode* cur = head;</span><br><span class="line">		<span class="keyword">while</span>(cur != <span class="literal">NULL</span> &amp;&amp; cur-&gt;next != <span class="literal">NULL</span>)&#123;</span><br><span class="line">			<span class="keyword">if</span>(cur-&gt;val == cur-&gt;next-&gt;val)&#123;</span><br><span class="line">				cur-&gt;next = cur-&gt;next-&gt;next;</span><br><span class="line">			&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">				cur = cur-&gt;next;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> head;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="344-反转字符串"   >
          <a href="#344-反转字符串" class="heading-link"><i class="fas fa-link"></i></a><a href="#344-反转字符串" class="headerlink" title="344. 反转字符串"></a>344. 反转字符串</h2>
      <p>编写一个函数，其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。</p>
<p>不要给另外的数组分配额外的空间，你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。</p>
<p>你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。</p>
<p><strong>示例 1：</strong><br>输入：[“h”,”e”,”l”,”l”,”o”]<br>输出：[“o”,”l”,”l”,”e”,”h”]</p>
<p><strong>示例 2：</strong><br>输入：[“H”,”a”,”n”,”n”,”a”,”h”]<br>输出：[“h”,”a”,”n”,”n”,”a”,”H”]</p>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/reverse-string" >https://leetcode-cn.com/problems/reverse-string</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">reverseString</span><span class="params">(vector&lt;<span class="keyword">char</span>&gt;&amp; s)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//给俩指针 一个指针指向第一个元素</span></span><br><span class="line">		<span class="comment">//再给一个指针给最后一个元素</span></span><br><span class="line">		<span class="keyword">int</span> i = <span class="number">0</span>; </span><br><span class="line">        <span class="keyword">int</span> end = s.<span class="built_in">size</span>()<span class="number">-1</span>;</span><br><span class="line">		<span class="comment">//直到俩指针相遇</span></span><br><span class="line">        <span class="keyword">while</span>(i&lt;=end)&#123;</span><br><span class="line">			<span class="comment">//交换首尾元素</span></span><br><span class="line">            <span class="keyword">char</span> temp = s[i];</span><br><span class="line">            s[i] = s[end];</span><br><span class="line">            s[end] = temp;</span><br><span class="line">			</span><br><span class="line">            i++;</span><br><span class="line">            end--;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="383-赎金信"   >
          <a href="#383-赎金信" class="heading-link"><i class="fas fa-link"></i></a><a href="#383-赎金信" class="headerlink" title="383. 赎金信"></a>383. 赎金信</h2>
      <p>给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串，判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成。<br>如果可以构成，返回 true ；否则返回 false。<br>(题目说明：为了不暴露赎金信字迹，要从杂志上搜索各个需要的字母，组成单词来表达意思。杂志字符串中的每个字符只能在赎金信字符串中使用一次。)</p>
<p><strong>示例 1：</strong><br>输入：ransomNote = “a”, magazine = “b”<br>输出：false</p>
<p><strong>示例 2：</strong><br>输入：ransomNote = “aa”, magazine = “ab”<br>输出：false</p>
<p><strong>示例 3：</strong><br>输入：ransomNote = “aa”, magazine = “aab”<br>输出：true</p>
<p>来源：力扣（LeetCode）<br>链接：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/ransom-note" >https://leetcode-cn.com/problems/ransom-note</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">canConstruct</span><span class="params">(string ransomNote, string magazine)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">int</span> count = <span class="number">0</span>; <span class="comment">//count用来标记r串中 可以找到的个数 </span></span><br><span class="line">		<span class="keyword">int</span> r_i = <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">int</span> r_len = ransomNote.<span class="built_in">size</span>();</span><br><span class="line">		<span class="keyword">int</span> m_end = magazine.<span class="built_in">size</span>();</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//从ran字符串中 一个一个判断</span></span><br><span class="line">		<span class="keyword">while</span>(r_i &lt; r_len)&#123;</span><br><span class="line">			<span class="keyword">int</span> pos = magazine.<span class="built_in">find</span>(ransomNote[r_i]);</span><br><span class="line">			<span class="keyword">if</span>( pos != string::npos)&#123;</span><br><span class="line">				count++;</span><br><span class="line">				</span><br><span class="line">				<span class="comment">//从mag中原地删除字符 </span></span><br><span class="line">				<span class="keyword">int</span> temp = magazine[pos];</span><br><span class="line">				magazine[pos] = magazine[m_end<span class="number">-1</span>];</span><br><span class="line">				magazine[m_end<span class="number">-1</span>] = temp;</span><br><span class="line">				</span><br><span class="line">				<span class="comment">//删除最后一个字符</span></span><br><span class="line">				magazine.<span class="built_in">erase</span>(m_end<span class="number">-1</span>);</span><br><span class="line">			&#125;</span><br><span class="line">			r_i++;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//如果都可以找到 就代表true </span></span><br><span class="line">		<span class="keyword">if</span>(count == ransomNote.<span class="built_in">size</span>())&#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="387-字符串中的第一个唯一字符"   >
          <a href="#387-字符串中的第一个唯一字符" class="heading-link"><i class="fas fa-link"></i></a><a href="#387-字符串中的第一个唯一字符" class="headerlink" title="387. 字符串中的第一个唯一字符"></a>387. 字符串中的第一个唯一字符</h2>
      <p>给定一个字符串，找到它的第一个不重复的字符，并返回它的索引。如果不存在，则返回 -1。</p>
<p><strong>示例：</strong><br>s = “leetcode”<br>返回 0</p>
<p>s = “loveleetcode”<br>返回 2</p>
<p><strong>提示：你可以假定该字符串只包含小写字母。</strong></p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">firstUniqChar</span><span class="params">(string s)</span> </span>&#123;</span><br><span class="line">        </span><br><span class="line">        unordered_map&lt;<span class="keyword">char</span>, <span class="keyword">int</span>&gt; frequency;</span><br><span class="line">        </span><br><span class="line">		<span class="comment">//将字符串中的每个字符 遇到一个自增</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">char</span> ch: s) &#123;</span><br><span class="line">            ++frequency[ch];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//从头到尾 输出第一个频度为1的</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; s.<span class="built_in">size</span>(); ++i) &#123;</span><br><span class="line">            <span class="keyword">if</span> (frequency[s[i]] == <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> i;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="268-丢失的数字"   >
          <a href="#268-丢失的数字" class="heading-link"><i class="fas fa-link"></i></a><a href="#268-丢失的数字" class="headerlink" title="268. 丢失的数字"></a>268. 丢失的数字</h2>
      <p>给定一个包含 [0, n] 中 n 个数的数组 nums ，找出 [0, n] 这个范围内没有出现在数组中的那个数。</p>
<p><strong>示例 1：</strong><br>输入：nums = [3,0,1]<br>输出：2<br>解释：n = 3，因为有 3 个数字，所以所有的数字都在范围 [0,3] 内。2 是丢失的数字，因为它没有出现在 nums 中。</p>
<p><strong>示例 2：</strong><br>输入：nums = [0,1]<br>输出：2<br>解释：n = 2，因为有 2 个数字，所以所有的数字都在范围 [0,2] 内。2 是丢失的数字，因为它没有出现在 nums 中。</p>
<p><strong>示例 3：</strong><br>输入：nums = [9,6,4,2,3,5,7,0,1]<br>输出：8<br>解释：n = 9，因为有 9 个数字，所以所有的数字都在范围 [0,9] 内。8 是丢失的数字，因为它没有出现在 nums 中。</p>
<p><strong>示例 4：</strong><br>输入：nums = [0]<br>输出：1<br>解释：n = 1，因为有 1 个数字，所以所有的数字都在范围 [0,1] 内。1 是丢失的数字，因为它没有出现在 nums 中。</p>
<p><strong>提示：</strong><br>n == nums.length<br>1 &lt;= n &lt;= 104<br>0 &lt;= nums[i] &lt;= n<br>nums 中的所有数字都 独一无二</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">missingNumber</span><span class="params">(vector&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">		<span class="comment">//首先从小到大进行排序</span></span><br><span class="line">		<span class="built_in">sort</span>(nums.<span class="built_in">begin</span>(), nums.<span class="built_in">end</span>()); </span><br><span class="line">        <span class="keyword">int</span> n = nums.<span class="built_in">size</span>();</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;nums.<span class="built_in">size</span>();i++)&#123;</span><br><span class="line">			<span class="comment">//如果n在数组当中 从小到大一次判断哪个数不在数组中</span></span><br><span class="line">            <span class="keyword">if</span>(nums[i] != i)&#123;</span><br><span class="line">                <span class="keyword">return</span> i;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">		<span class="comment">//否则没有找到 那么就是n这个数</span></span><br><span class="line">        <span class="keyword">return</span> n;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="414-第三大的数"   >
          <a href="#414-第三大的数" class="heading-link"><i class="fas fa-link"></i></a><a href="#414-第三大的数" class="headerlink" title="414. 第三大的数"></a>414. 第三大的数</h2>
      <p>给你一个非空数组，返回此数组中 第三大的数 。如果不存在，则返回数组中最大的数。</p>
<p><strong>示例 1：</strong><br>输入：[3, 2, 1]<br>输出：1<br>解释：第三大的数是 1 。</p>
<p><strong>示例 2：</strong><br>输入：[1, 2]<br>输出：2<br>解释：第三大的数不存在, 所以返回最大的数 2 。</p>
<p><strong>示例 3：</strong><br>输入：[2, 2, 3, 1]<br>输出：1<br>解释：注意，要求返回第三大的数，是指在所有不同数字中排第三大的数。<br>此例中存在两个值为 2 的数，它们都排第二。在所有不同数字中排第三大的数为 1 。</p>
<p><strong>提示：</strong><br>1 &lt;= nums.length &lt;= 104<br>-231 &lt;= nums[i] &lt;= 231 - 1</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">thirdMax</span><span class="params">(vector&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">	<span class="comment">//对所给数组进行从小到大排序</span></span><br><span class="line">	<span class="built_in">sort</span>(nums.<span class="built_in">begin</span>(), nums.<span class="built_in">end</span>()); </span><br><span class="line"></span><br><span class="line">	set&lt;<span class="keyword">int</span>&gt; s;</span><br><span class="line">	<span class="keyword">int</span> len = nums.<span class="built_in">size</span>();</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;len; i++)&#123;</span><br><span class="line">		<span class="comment">//将元素加入无重复的集合中</span></span><br><span class="line">		s.<span class="built_in">insert</span>( nums[i] );</span><br><span class="line">	&#125;	</span><br><span class="line"></span><br><span class="line">	<span class="comment">//清空vector</span></span><br><span class="line">	nums.<span class="built_in">clear</span>();</span><br><span class="line">	<span class="keyword">for</span>(set&lt;<span class="keyword">int</span>&gt;::iterator it=s.<span class="built_in">begin</span>();it!=s.<span class="built_in">end</span>();it++)&#123;</span><br><span class="line">		<span class="comment">//将集合中的元素 加入数组中</span></span><br><span class="line">		nums.<span class="built_in">push_back</span>(*it);		</span><br><span class="line">	&#125; </span><br><span class="line">	</span><br><span class="line">	<span class="comment">//如果数组的长度小于3 那么返回数组的最后一个元素</span></span><br><span class="line">	<span class="keyword">if</span>(nums.<span class="built_in">size</span>()&lt;<span class="number">3</span>) <span class="keyword">return</span> nums[nums.<span class="built_in">size</span>()<span class="number">-1</span>];</span><br><span class="line"></span><br><span class="line">	<span class="comment">//否则返回倒数第三个</span></span><br><span class="line">	<span class="keyword">return</span> nums[nums.<span class="built_in">size</span>()<span class="number">-3</span>];</span><br><span class="line">&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h2 id="1004-母牛的故事"   >
          <a href="#1004-母牛的故事" class="heading-link"><i class="fas fa-link"></i></a><a href="#1004-母牛的故事" class="headerlink" title="1004: 母牛的故事"></a>1004: 母牛的故事</h2>
      <p><strong>题目描述</strong><br>有一头母牛，它每年年初生一头小母牛。每头小母牛从第四个年头开始，每年年初也生一头小母牛。请编程实现在第n年的时候，共有多少头母牛？</p>
<p><strong>输入</strong><br>输入数据由多个测试实例组成，每个测试实例占一行，包括**一个整数n(0&lt;n&lt;55)**，n的含义如题目中描述。<br>n=0表示输入数据的结束，不做处理。</p>
<p><strong>输出</strong><br>对于每个测试实例，输出在第n年的时候母牛的数量。<br>每个输出占一行。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">fun</span><span class="params">(<span class="keyword">int</span> n)</span></span>&#123;</span><br><span class="line"> 	<span class="keyword">int</span> a[N];</span><br><span class="line"> 	<span class="keyword">int</span> i;</span><br><span class="line">	<span class="comment">//避免索引错位 因此直接让a[1]=1...</span></span><br><span class="line"> 	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>; i&lt;=<span class="number">4</span>; i++)&#123;</span><br><span class="line"> 		a[i] = i;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//索引从第五年开始 状态转移方程是 ai = ai-1 + ai-3</span></span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">5</span>; i&lt;=n; i++)&#123;</span><br><span class="line">		a[i] = a[i<span class="number">-1</span>] + a[i<span class="number">-3</span>];</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//返回该数的数组位置</span></span><br><span class="line">	<span class="keyword">return</span> a[n];	</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> y = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span>(cin &gt;&gt; y)&#123;</span><br><span class="line">		<span class="keyword">if</span>(y == <span class="number">0</span>)</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		cout &lt;&lt; <span class="built_in">fun</span>(y) &lt;&lt;endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="434-字符串中的单词数"   >
          <a href="#434-字符串中的单词数" class="heading-link"><i class="fas fa-link"></i></a><a href="#434-字符串中的单词数" class="headerlink" title="434. 字符串中的单词数"></a>434. 字符串中的单词数</h2>
      <p>统计字符串中的单词个数，这里的单词指的是连续的不是空格的字符。</p>
<p>请注意，你可以假定字符串里不包括任何不可打印的字符。</p>
<p><strong>示例:</strong></p>
<p>输入: “Hello, my name is John”<br>输出: 5<br>解释: 这里的单词是指连续的不是空格的字符，所以 “Hello,” 算作 1 个单词。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">countSegments</span><span class="params">(string s)</span> </span>&#123;</span><br><span class="line">  		<span class="comment">//flag stands for if it is the Number </span></span><br><span class="line">  	    <span class="comment">//count stands for the number of Num</span></span><br><span class="line">		<span class="keyword">int</span> flag=<span class="number">0</span>,count = <span class="number">0</span>; </span><br><span class="line">		</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; s[i] != <span class="string">&#x27;\0&#x27;</span>; i++)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span>(s[i] == <span class="string">&#x27; &#x27;</span>)&#123;</span><br><span class="line">				flag = <span class="number">0</span>;</span><br><span class="line">			&#125;<span class="keyword">else</span> <span class="keyword">if</span>(flag == <span class="number">0</span>)&#123;	</span><br><span class="line">                count++;</span><br><span class="line">				flag = <span class="number">1</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	  	<span class="keyword">return</span> count;</span><br><span class="line">&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="392-判断子序列"   >
          <a href="#392-判断子序列" class="heading-link"><i class="fas fa-link"></i></a><a href="#392-判断子序列" class="headerlink" title="392. 判断子序列"></a>392. 判断子序列</h2>
      <p>给定字符串 s 和 t ，判断 s 是否为 t 的子序列。<br>字符串的一个子序列是原始字符串删除一些（也可以不删除）字符而不改变剩余字符相对位置形成的新字符串。（例如，”ace”是”abcde”的一个子序列，而”aec”不是）。</p>
<p><strong>示例 1：</strong><br>输入：s = “abc”, t = “ahbgdc”<br>输出：true</p>
<p><strong>示例 2：</strong><br>输入：s = “axc”, t = “ahbgdc”<br>输出：false<br> <br><strong>提示：</strong><br>0 &lt;= s.length &lt;= 100<br>0 &lt;= t.length &lt;= 10^4<br>两个字符串都只由小写字符组成。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isSubsequence</span><span class="params">(string s, string t)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">bool</span> flag = <span class="literal">true</span>;</span><br><span class="line">	<span class="keyword">int</span> i,j = <span class="number">-1</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; s[i] != <span class="string">&#x27;\0&#x27;</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="comment">//从s中找各个字符 对应在t中的位置</span></span><br><span class="line">		j = t.<span class="built_in">find</span>(s[i]);		</span><br><span class="line">		<span class="keyword">if</span>(j != string::npos)&#123;</span><br><span class="line">			<span class="comment">//如果没找到 就返回false </span></span><br><span class="line">			<span class="comment">//否则截取找到的位置 下一个位置到结束</span></span><br><span class="line">			<span class="comment">//继续往后找</span></span><br><span class="line">			t = t.<span class="built_in">substr</span>(j+<span class="number">1</span>);</span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			flag = <span class="literal">false</span>;</span><br><span class="line">			<span class="keyword">return</span> flag;			</span><br><span class="line">		&#125;	</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> flag;</span><br><span class="line">&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="551-学生出勤记录-I"   >
          <a href="#551-学生出勤记录-I" class="heading-link"><i class="fas fa-link"></i></a><a href="#551-学生出勤记录-I" class="headerlink" title="551. 学生出勤记录 I"></a>551. 学生出勤记录 I</h2>
      <p>给定一个字符串来代表一个学生的出勤记录，这个记录仅包含以下三个字符：</p>
<p>‘A’ : Absent，缺勤<br>‘L’ : Late，迟到<br>‘P’ : Present，到场<br>如果一个学生的出勤记录中不超过一个’A’(缺勤)并且不超过两个连续的’L’(迟到),那么这个学生会被奖赏。</p>
<p>你需要根据这个学生的出勤记录判断他是否会被奖赏。</p>
<p><strong>示例 1:</strong><br>输入: “PPALLP”<br>输出: True</p>
<p><strong>示例 2:</strong><br>输入: “PPALLL”<br>输出: False</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">   </span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">checkRecord</span><span class="params">(string s)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">bool</span> flag = <span class="literal">false</span>;</span><br><span class="line">	<span class="keyword">int</span> countA=<span class="number">0</span>, countL=s.<span class="built_in">find</span>(<span class="string">&quot;LLL&quot;</span>);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;s.<span class="built_in">size</span>(); i++)&#123;</span><br><span class="line">		<span class="keyword">if</span>(s[i] == <span class="string">&#x27;A&#x27;</span>)&#123;</span><br><span class="line">			<span class="comment">//对于不用连续出现的A 通过统计即可</span></span><br><span class="line">            countA++;</span><br><span class="line">        &#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//对于连续出现L 可以看成是否出现 不满足题目要求的“字串” &quot;LLL&quot;即可</span></span><br><span class="line">    <span class="keyword">if</span>(countL==string::npos &amp;&amp; countA &lt;= <span class="number">1</span>)&#123;</span><br><span class="line">        flag = <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="keyword">return</span> flag;</span><br><span class="line">&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="面试题-01-03-URL化"   >
          <a href="#面试题-01-03-URL化" class="heading-link"><i class="fas fa-link"></i></a><a href="#面试题-01-03-URL化" class="headerlink" title="面试题 01.03. URL化"></a>面试题 01.03. URL化</h2>
      <p>URL化。编写一种方法，将字符串中的空格全部替换为%20。<br>假定该字符串尾部有足够的空间存放新增字符，并且知道字符串的“真实”长度。<br>（注：用Java实现的话，请使用字符数组实现，以便直接在数组上操作。）</p>
<p><strong>示例 1：</strong><br>输入：”Mr John Smith    “, 13<br>输出：”Mr%20John%20Smith”<br> <br><strong>示例 2：</strong><br>输入：”               “, 5<br>输出：”%20%20%20%20%20”</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">   <span class="function">string <span class="title">replaceSpaces</span><span class="params">(string S, <span class="keyword">int</span> length)</span> </span>&#123;</span><br><span class="line">        string s = <span class="string">&quot;&quot;</span>; <span class="comment">//用一个新的字符串来装</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;length; i++)&#123;</span><br><span class="line">            <span class="keyword">if</span>(S[i] == <span class="string">&#x27; &#x27;</span>)&#123;</span><br><span class="line">                s += <span class="string">&quot;%20&quot;</span>;</span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                s.<span class="built_in">push_back</span>(S[i]);</span><br><span class="line">            &#125;         </span><br><span class="line">        &#125;</span><br><span class="line">        cout &lt;&lt; s;</span><br><span class="line">        <span class="keyword">return</span> s;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></div></figure>


        <h2 id="459-重复的子字符串-巧妙"   >
          <a href="#459-重复的子字符串-巧妙" class="heading-link"><i class="fas fa-link"></i></a><a href="#459-重复的子字符串-巧妙" class="headerlink" title="459. 重复的子字符串(巧妙)"></a>459. 重复的子字符串(巧妙)</h2>
      <p>给定一个非空的字符串，判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母，并且长度不超过10000。</p>
<p><strong>示例 1:</strong><br>输入: “abab”<br>输出: True<br>解释: 可由子字符串 “ab” 重复两次构成。</p>
<p><strong>示例 2:</strong><br>输入: “aba”<br>输出: False</p>
<p><strong>示例 3:</strong><br>输入: “abcabcabcabc”<br>输出: True<br>解释: 可由子字符串 “abc” 重复四次构成。 (或者子字符串 “abcabc” 重复两次构成。)</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">repeatedSubstringPattern</span><span class="params">(string s)</span> </span>&#123;  </span><br><span class="line">		<span class="comment">//如果是可用 子串重复 的串（比如abab）</span></span><br><span class="line">		<span class="comment">//那么本身轮回一圈后 下次找一定可以在 小于这个串长度的位置里</span></span><br><span class="line">		<span class="comment">//找到自身的串首次出现的位置</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">//否则如果不是可重复的（比如ab）</span></span><br><span class="line">		<span class="comment">//本身轮回一圈后 下次再找到自己的串ab 一定不在自身的长度以内 abab</span></span><br><span class="line">		<span class="keyword">return</span> (s + s).<span class="built_in">find</span>(s, <span class="number">1</span>) != s.<span class="built_in">size</span>();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h2 id="506-相对名次"   >
          <a href="#506-相对名次" class="heading-link"><i class="fas fa-link"></i></a><a href="#506-相对名次" class="headerlink" title="506. 相对名次"></a>506. 相对名次</h2>
      <p>给出 N 名运动员的成绩，找出他们的相对名次并授予前三名对应的奖牌。<br>前三名运动员将会被分别授予 “金牌”，“银牌” 和“ 铜牌”（”Gold Medal”, “Silver Medal”, “Bronze Medal”）。<br>(注：分数越高的选手，排名越靠前。)</p>
<p>示例 1:<br>输入: [5, 4, 3, 2, 1]<br>输出: [“Gold Medal”, “Silver Medal”, “Bronze Medal”, “4”, “5”]<br>解释: 前三名运动员的成绩为前三高的，因此将会分别被授予 “金牌”，“银牌”和“铜牌” (“Gold Medal”, “Silver Medal” and “Bronze Medal”).<br>余下的两名运动员，我们只需要通过他们的成绩计算将其相对名次即可。</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    </span><br><span class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">bool</span> <span class="title">cmp</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span></span>&#123;</span><br><span class="line">	<span class="keyword">return</span> a&gt;b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">vector&lt;string&gt; <span class="title">findRelativeRanks</span><span class="params">(vector&lt;<span class="keyword">int</span>&gt;&amp; score)</span> </span>&#123;</span><br><span class="line">	vector&lt;string&gt; v;</span><br><span class="line">	unordered_map&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; m;</span><br><span class="line">	<span class="keyword">int</span> num = <span class="number">1</span>; <span class="comment">//根据分数 从高（第一名）到低进行排名</span></span><br><span class="line">	string gold = <span class="string">&quot;Gold Medal&quot;</span>;</span><br><span class="line">	string silver = <span class="string">&quot;Silver Medal&quot;</span>;</span><br><span class="line">	string bronze = <span class="string">&quot;Bronze Medal&quot;</span>;</span><br><span class="line">     </span><br><span class="line">	vector&lt;<span class="keyword">int</span>&gt; score1 = score; <span class="comment">//新建一个数组 用来记录分数对应的名词</span></span><br><span class="line">	</span><br><span class="line">	<span class="built_in">sort</span>(score1.<span class="built_in">begin</span>(), score1.<span class="built_in">end</span>(), cmp); <span class="comment">//最高分的排在前面</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;score1.<span class="built_in">size</span>(); i++)&#123;</span><br><span class="line">		m[score1[i]] = num++; <span class="comment">//开始记录</span></span><br><span class="line">	&#125;</span><br><span class="line">		</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;score.<span class="built_in">size</span>(); i++)&#123;</span><br><span class="line">		<span class="keyword">int</span> n = m[score[i]]; <span class="comment">//根据自己的元素 原地取对应的排名 </span></span><br><span class="line"></span><br><span class="line">		<span class="comment">//修改前三名的string（金银铜）</span></span><br><span class="line">		<span class="keyword">if</span>(n == <span class="number">1</span>)&#123;</span><br><span class="line">			v.<span class="built_in">push_back</span>(gold);</span><br><span class="line">			<span class="keyword">continue</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(n == <span class="number">2</span>)&#123;</span><br><span class="line">			v.<span class="built_in">push_back</span>(silver);</span><br><span class="line">			<span class="keyword">continue</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(n == <span class="number">3</span>)&#123;</span><br><span class="line">			v.<span class="built_in">push_back</span>(bronze);</span><br><span class="line">			<span class="keyword">continue</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		v.<span class="built_in">push_back</span>(<span class="built_in">to_string</span>(n)); </span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(string str:v)&#123;</span><br><span class="line">		cout &lt;&lt; str &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> v; </span><br><span class="line">&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>

</div><footer class="post-footer"><div class="post-ending ending"><div class="ending__text">------ 本文结束，感谢您的阅读 ------</div></div><div class="post-copyright copyright"><div class="copyright-author"><span class="copyright-author__name">本文作者: </span><span class="copyright-author__value"><a href="https://bruce-jj.gitee.io/blog">何水浪</a></span></div><div class="copyright-link"><span class="copyright-link__name">本文链接: </span><span class="copyright-link__value"><a href="https://bruce-jj.gitee.io/blog/2021/03/11/C_C++%E5%8A%9B%E6%89%A3LeetCode%E8%BD%AC%E8%BD%BD/">https://bruce-jj.gitee.io/blog/2021/03/11/C_C++%E5%8A%9B%E6%89%A3LeetCode%E8%BD%AC%E8%BD%BD/</a></span></div><div class="copyright-notice"><span class="copyright-notice__name">版权声明: </span><span class="copyright-notice__value">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" rel="external nofollow" target="_blank">BY-NC-SA</a> 许可协议。转载请注明出处！</span></div></div><div class="post-tags"><span class="post-tags-item"><span class="post-tags-item__icon"><i class="fas fa-tag"></i></span><a class="post-tags-item__link" href="https://bruce-jj.gitee.io/blog/tags/C%E8%AF%AD%E8%A8%80/">C语言</a></span></div><nav class="post-paginator paginator"><div class="paginator-prev"><a class="paginator-prev__link" href="/blog/2021/03/11/C_C++%20%E7%BC%96%E7%A8%8B%E5%B8%B8%E8%A7%81%E4%BB%A3%E7%A0%81/"><span class="paginator-prev__icon"><i class="fas fa-angle-left"></i></span><span class="paginator-prev__text">C/C++ 编程常见代码</span></a></div></nav></footer></div></div><div class="comments" id="comments"><div id="valine-container"></div></div></div><div class="sidebar-wrap" id="sidebar-wrap"><aside class="sidebar" id="sidebar"><div class="sidebar-nav"><span class="sidebar-nav-toc current">文章目录</span><span class="sidebar-nav-ov">站点概览</span></div><section class="sidebar-toc"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#27-%E5%8E%9F%E5%9C%B0%E7%A7%BB%E5%87%BA%E6%9F%90%E6%95%B0%E5%90%8E%E7%9A%84%E6%95%B0%E7%BB%84"><span class="toc-number">1.</span> <span class="toc-text">
          27.原地移出某数后的数组</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#58-%E8%BF%94%E5%9B%9E%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E5%8D%95%E8%AF%8D%E7%9A%84%E9%95%BF%E5%BA%A6"><span class="toc-number">2.</span> <span class="toc-text">
          58.返回最后一个单词的长度</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E5%AE%9E%E7%8E%B0%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97"><span class="toc-number">3.</span> <span class="toc-text">
          动态规划实现斐波那契数列</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%90%88%E5%B9%B6%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E4%BD%BF%E5%85%B6%E6%9C%89%E5%BA%8F"><span class="toc-number">4.</span> <span class="toc-text">
          合并有序数组使其有序</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%89%BE%E5%88%B0%E5%80%BC%E7%9A%84%E7%B4%A2%E5%BC%95-%E6%88%96%E8%80%85%E7%90%86%E5%BA%94%E6%8F%92%E5%85%A5%E7%9A%84%E4%BD%8D%E7%BD%AE"><span class="toc-number">5.</span> <span class="toc-text">
          找到值的索引 或者理应插入的位置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#121-%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BA"><span class="toc-number">6.</span> <span class="toc-text">
          121. 买卖股票的最佳时机</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#202-%E5%BF%AB%E4%B9%90%E6%95%B0"><span class="toc-number">7.</span> <span class="toc-text">
          202. 快乐数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#136-%E5%8F%AA%E5%87%BA%E7%8E%B0%E4%B8%80%E6%AC%A1%E7%9A%84%E6%95%B0%E5%AD%97"><span class="toc-number">8.</span> <span class="toc-text">
          136. 只出现一次的数字</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#204-%E8%AE%A1%E6%95%B0%E8%B4%A8%E6%95%B0"><span class="toc-number">9.</span> <span class="toc-text">
          204. 计数质数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#217-%E5%AD%98%E5%9C%A8%E9%87%8D%E5%A4%8D%E5%85%83%E7%B4%A0"><span class="toc-number">10.</span> <span class="toc-text">
          217. 存在重复元素</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#258-%E5%90%84%E4%BD%8D%E7%9B%B8%E5%8A%A0"><span class="toc-number">11.</span> <span class="toc-text">
          258. 各位相加</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#28-%E5%AE%9E%E7%8E%B0-strStr"><span class="toc-number">12.</span> <span class="toc-text">
          28.实现 strStr()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#83-%E5%88%A0%E9%99%A4%E6%8E%92%E5%BA%8F%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E9%87%8D%E5%A4%8D%E5%85%83%E7%B4%A0"><span class="toc-number">13.</span> <span class="toc-text">
          83. 删除排序链表中的重复元素</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#344-%E5%8F%8D%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">14.</span> <span class="toc-text">
          344. 反转字符串</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#383-%E8%B5%8E%E9%87%91%E4%BF%A1"><span class="toc-number">15.</span> <span class="toc-text">
          383. 赎金信</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#387-%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%94%AF%E4%B8%80%E5%AD%97%E7%AC%A6"><span class="toc-number">16.</span> <span class="toc-text">
          387. 字符串中的第一个唯一字符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#268-%E4%B8%A2%E5%A4%B1%E7%9A%84%E6%95%B0%E5%AD%97"><span class="toc-number">17.</span> <span class="toc-text">
          268. 丢失的数字</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#414-%E7%AC%AC%E4%B8%89%E5%A4%A7%E7%9A%84%E6%95%B0"><span class="toc-number">18.</span> <span class="toc-text">
          414. 第三大的数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1004-%E6%AF%8D%E7%89%9B%E7%9A%84%E6%95%85%E4%BA%8B"><span class="toc-number">19.</span> <span class="toc-text">
          1004: 母牛的故事</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#434-%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E5%8D%95%E8%AF%8D%E6%95%B0"><span class="toc-number">20.</span> <span class="toc-text">
          434. 字符串中的单词数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#392-%E5%88%A4%E6%96%AD%E5%AD%90%E5%BA%8F%E5%88%97"><span class="toc-number">21.</span> <span class="toc-text">
          392. 判断子序列</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#551-%E5%AD%A6%E7%94%9F%E5%87%BA%E5%8B%A4%E8%AE%B0%E5%BD%95-I"><span class="toc-number">22.</span> <span class="toc-text">
          551. 学生出勤记录 I</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%9D%A2%E8%AF%95%E9%A2%98-01-03-URL%E5%8C%96"><span class="toc-number">23.</span> <span class="toc-text">
          面试题 01.03. URL化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#459-%E9%87%8D%E5%A4%8D%E7%9A%84%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2-%E5%B7%A7%E5%A6%99"><span class="toc-number">24.</span> <span class="toc-text">
          459. 重复的子字符串(巧妙)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#506-%E7%9B%B8%E5%AF%B9%E5%90%8D%E6%AC%A1"><span class="toc-number">25.</span> <span class="toc-text">
          506. 相对名次</span></a></li></ol></section><!-- ov = overview--><section class="sidebar-ov hide"><div class="sidebar-ov-author"><div class="sidebar-ov-author__avatar"><img class="sidebar-ov-author__avatar_img" src="/blog/images/avatar.jpg" alt="avatar"></div><p class="sidebar-ov-author__text">C'est la vie</p></div><div class="sidebar-ov-state"><a class="sidebar-ov-state-item sidebar-ov-state-item--posts" href="/blog/archives/"><div class="sidebar-ov-state-item__count">48</div><div class="sidebar-ov-state-item__name">归档</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--categories" href="/blog/categories/"><div class="sidebar-ov-state-item__count">19</div><div class="sidebar-ov-state-item__name">分类</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--tags" href="/blog/tags/"><div class="sidebar-ov-state-item__count">20</div><div class="sidebar-ov-state-item__name">标签</div></a></div><div class="sidebar-ov-cc"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" target="_blank" rel="noopener" data-popover="知识共享许可协议" data-popover-pos="up"><img src="/blog/images/cc-by-nc-sa.svg"></a></div></section><div class="sidebar-reading"><div class="sidebar-reading-info"><span class="sidebar-reading-info__text">你已阅读了 </span><span class="sidebar-reading-info__num">0</span><span class="sidebar-reading-info__perc">%</span></div><div class="sidebar-reading-line"></div></div></aside></div><div class="clearfix"></div></div></main><footer class="footer" id="footer"><div class="footer-inner"><div><span>Copyright © 2021</span><span class="footer__icon"><i class="fas fa-heart"></i></span><span>何水浪</span></div><div><span>由 <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a> 强力驱动</span><span> v5.4.0</span><span class="footer__devider">|</span><span>主题 - <a href="https://github.com/liuyib/hexo-theme-stun/" title="Stun" target="_blank" rel="noopener">Stun</a></span><span> v2.6.2</span></div></div></footer><div class="loading-bar" id="loading-bar"><div class="loading-bar__progress"></div></div><div class="back2top" id="back2top"><span class="back2top__icon"><i class="fas fa-rocket"></i></span></div></div><div class="search-mask"></div><div class="search-popup"><span class="search-close"></span><div class="search-input"><input placeholder="搜索文章（支持多关键词，请用空格分隔）"></div><div class="search-results"></div></div><script src="https://cdn.jsdelivr.net/npm/jquery@v3.4.1/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.ui.min.js"></script><script>function initSearch() {
  var isXML = true;
  var search_path = 'search.json';

  if (!search_path) {
    search_path = 'search.xml';
  } else if (/json$/i.test(search_path)) {
    isXML = false;
  }

  var path = '/blog/' + search_path;
  $.ajax({
    url: path,
    dataType: isXML ? 'xml' : 'json',
    async: true,
    success: function (res) {
      var datas = isXML ? $('entry', res).map(function () {
        // 将 XML 转为 JSON
        return {
          title: $('title', this).text(),
          content: $('content', this).text(),
          url: $('url', this).text()
        };
      }).get() : res;
      var $input = $('.search-input input');
      var $result = $('.search-results');
      // 搜索对象（标题、内容）的权重，影响显示顺序
      var WEIGHT = { title: 100, content: 1 };
      var searchPost = function () {
        var searchText = $input.val().toLowerCase().trim();
        // 根据空白字符分隔关键字
        var keywords = searchText.split(/[\s]+/);
        // 搜索结果
        var matchPosts = [];

        // 有多个关键字时，将原文字整个保存下来
        if (keywords.length > 1) {
          keywords.push(searchText);
        }
        // 防止未输入字符时搜索
        if (searchText.length > 0) {
          datas.forEach(function (data) {
            var isMatch  = false;
            // 没有标题的文章使用预设的 i18n 变量代替
            var title = (data.title && data.title.trim()) || '[ 文章无标题 ]';
            var titleLower = title && title.toLowerCase();
            // 删除 HTML 标签 和 所有空白字符
            var content = data.content && data.content.replace(/<[^>]+>/g, '');
            var contentLower = content && content.toLowerCase();
            // 删除重复的 /
            var postURL = data.url && decodeURI(data.url).replace(/\/{2,}/g, '/');
            // 标题中匹配到的关键词
            var titleHitSlice = [];
            // 内容中匹配到的关键词
            var contentHitSlice = [];

            keywords.forEach(function (keyword) {
              /**
              * 获取匹配的关键词的索引
              * @param {String} keyword 要匹配的关键字
              * @param {String} text 原文字
              * @param {Boolean} caseSensitive 是否区分大小写
              * @param {Number} weight 匹配对象的权重。权重大的优先显示
              * @return {Array}
              */
              function getIndexByword (word, text, caseSensitive, weight) {
                if (!word || !text) {
                  return [];
                };

                var startIndex = 0; // 每次匹配的开始索引
                var index = -1;     // 匹配到的索引值
                var result = [];    // 匹配结果

                if (!caseSensitive) {
                  word = word.toLowerCase();
                  text = text.toLowerCase();
                }

                while((index = text.indexOf(word, startIndex)) !== -1) {
                  var hasMatch = false;
                  // 索引位置相同的关键词，保留长度较长的
                  titleHitSlice.forEach(function (hit) {
                    if (hit.index === index && hit.word.length < word.length) {
                      hit.word = word;
                      hasMatch = true;
                    }
                  });
                  startIndex = index + word.length;
                  !hasMatch && result.push({ index: index, word: word, weight: weight });
                }
                return result;
              }
              titleHitSlice = titleHitSlice.concat(getIndexByword(keyword, titleLower, false, WEIGHT.title));
              contentHitSlice = contentHitSlice.concat(getIndexByword(keyword, contentLower, false, WEIGHT.content));
            });

            var hitTitle = titleHitSlice.length;
            var hitContent = contentHitSlice.length;

            if (hitTitle > 0 || hitContent > 0) {
              isMatch = true;
            }
            if (isMatch) {
              ;[titleHitSlice, contentHitSlice].forEach(function (hit) {
                // 按照匹配文字的索引的递增顺序排序
                hit.sort(function (left, right) {
                  return left.index - right.index;
                });
              });
              /**
              * 给文本中匹配到的关键词添加标记，从而进行高亮显示
              * @param {String} text 原文本
              * @param {Array} hitSlice 匹配项的索引信息
              * @param {Number} start 开始索引
              * @param {Number} end 结束索引
              * @return {String}
              */
              function highlightKeyword (text, hitSlice, start, end) {
                if (!text || !hitSlice || !hitSlice.length) {
                  return;
                }

                var result = '';
                var startIndex = start;
                var endIndex = end;
                hitSlice.forEach(function (hit) {
                  if (hit.index < startIndex) {
                    return;
                  }

                  var hitWordEnd = hit.index + hit.word.length;
                  result += text.slice(startIndex, hit.index);
                  result += '<b>' + text.slice(hit.index, hitWordEnd) + '</b>';
                  startIndex = hitWordEnd;
                });
                result += text.slice(startIndex, endIndex);
                return result;
              }

              var postData = {};
              // 文章总的搜索权重
              var postWeight = titleHitSlice.length * WEIGHT.title + contentHitSlice.length * WEIGHT.content;
              // 标记匹配关键词后的标题
              var postTitle = highlightKeyword(title, titleHitSlice, 0, title.length) || title;
              // 标记匹配关键词后的内容
              var postContent;
              // 显示内容的长度
              var SHOW_WORD_LENGTH = 200;
              // 命中关键词前的字符显示长度
              var SHOW_WORD_FRONT_LENGTH = 20;
              var SHOW_WORD_END_LENGTH = SHOW_WORD_LENGTH - SHOW_WORD_FRONT_LENGTH;

              // 截取匹配的第一个字符，前后共 200 个字符来显示
              if (contentHitSlice.length > 0) {
                var firstIndex = contentHitSlice[0].index;
                var start = firstIndex > SHOW_WORD_FRONT_LENGTH ? firstIndex - SHOW_WORD_FRONT_LENGTH : 0;
                var end = firstIndex + SHOW_WORD_END_LENGTH;
                postContent = highlightKeyword(content, contentHitSlice, start, end);
              } else { // 未匹配到内容，直接截取前 200 个字符来显示
                postContent = content.slice(0, SHOW_WORD_LENGTH);
              }
              postData.title = postTitle;
              postData.content = postContent;
              postData.url = postURL;
              postData.weight = postWeight;
              matchPosts.push(postData);
            }
          });
        }

        var resultInnerHtml = '';
        if (matchPosts.length) {
          // 按权重递增的顺序排序，使权重大的优先显示
          matchPosts.sort(function (left, right) {
            return right.weight - left.weight;
          });
          resultInnerHtml += '<ul>';
          matchPosts.forEach(function (post) {
            resultInnerHtml += '<li><a class="search-results-title" href="' + post.url + '">';
            resultInnerHtml += post.title;
            resultInnerHtml += '</a><div class="search-results-content">';
            resultInnerHtml += post.content;
            resultInnerHtml += '</div></li>';
          });
          resultInnerHtml += '</ul>';
        } else {
          resultInnerHtml += '<div class="search-results-none"><i class="far fa-meh"></i></div>';
        }
        $result.html(resultInnerHtml);
      };
      $input.on('input', searchPost);
      $input.on('keyup', function (e) {
        if (e.keyCode === Stun.utils.codeToKeyCode('Enter')) {
          searchPost();
        }
      });
    }
  });
}

function closeSearch () {
  $('body').css({ overflow: 'auto' });
  $('.search-popup').css({ display: 'none' });
  $('.search-mask').css({ display: 'none' });
}

window.addEventListener('DOMContentLoaded', function () {
  Stun.utils.pjaxReloadLocalSearch = function () {
    $('.header-nav-search').on('click', function (e) {
      e.stopPropagation();
      $('body').css('overflow', 'hidden');
      $('.search-popup')
        .velocity('stop')
        .velocity('transition.expandIn', {
          duration: 300,
          complete: function () {
            $('.search-popup input').focus();
          }
        });
      $('.search-mask')
        .velocity('stop')
        .velocity('transition.fadeIn', {
          duration: 300
        });

      initSearch();
    });
    $('.search-mask, .search-close').on('click', function () {
      closeSearch();
    });
    $(document).on('keydown', function (e) {
      // Escape <=> 27
      if (e.keyCode === Stun.utils.codeToKeyCode('Escape')) {
        closeSearch();
      }
    });
  };

  Stun.utils.pjaxReloadLocalSearch();
}, false);

function safeOpenUrl(url) {
  var newTab = window.open();
  newTab.opener = null;
  newTab.location = url;
}

function extSearch(engine) {
  var engines = {
    google: 'https://www.google.com/search?q=',
    bing: 'https://cn.bing.com/search?q=',
    baidu: 'https://www.baidu.com/s?ie=UTF-8&wd=',
  };
  var host = window.location.host;
  var query = $('.search-input input').val().toLowerCase().trim();
  var uri = engines[engine] + query + ' site:' + host;

  if (query) {
    safeOpenUrl(uri);
  } else {
    Stun.utils.popAlert('warning', '请输入字符');
  }
}

var assistSearchList = window.CONFIG.assistSearch;

if (Array.isArray(assistSearchList)) {
  assistSearchList.forEach(function (name) {
    document.querySelector('.search-btns-item--' + name).addEventListener('click', function () {
      extSearch(name);
    }, false);
  });
}</script><script src="https://cdn.jsdelivr.net/npm/leancloud-storage@latest/dist/av-min.js"></script><script src="https://cdn.jsdelivr.net/npm/valine@latest/dist/Valine.min.js"></script><script>function loadValine () {
  var GUEST_INFO = ['nick', 'mail', 'link'];
  var guest_info = 'nick';

  guest_info = guest_info.split(',').filter(function(item) {
    return GUEST_INFO.indexOf(item) > -1;
  });
  new Valine({
    el: '#valine-container',
    appId: 'fJ4fFnieffDVKy4Jkn16aHdO-gzGzoHsz',
    appKey: 'qfVo2CUC2pJ3udFzLFnd0GBM',
    notify: true,
    verify: true,
    placeholder: '说点啥再走呗~爱你',
    avatar: 'mp',
    meta: guest_info,
    pageSize: '10' || 10,
    visitor: false,
    recordIP: false,
    lang: '' || 'zh-cn',
    path: window.location.pathname
  });
}

if (false) {
  loadValine();
} else {
  window.addEventListener('DOMContentLoaded', loadValine, false);
}</script><script src="/blog/js/utils.js?v=2.6.2"></script><script src="/blog/js/stun-boot.js?v=2.6.2"></script><script src="/blog/js/scroll.js?v=2.6.2"></script><script src="/blog/js/header.js?v=2.6.2"></script><script src="/blog/js/sidebar.js?v=2.6.2"></script><script type="application/json" src="/blog/search.json"></script></body></html>